package com.xq.tmall.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xq.tmall.dao.StockSectorDao;
import com.xq.tmall.entity.StockSector;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 行业板块
 */
@Service
public class StockSectorService {

    @Resource
    private StockSectorDao stockSectorDao;

    public void batchUpdate(String json) {
        List<StockSector> stockSectors = new ArrayList<>();
        JSONObject jsonObject = JSON.parseObject(json);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray jsonArray = data.getJSONArray("diff");
        for (int i = 0; i < jsonArray.size(); i++) {
            StockSector stockSector = new StockSector(jsonArray.getJSONObject(i));
            stockSectors.add(stockSector);
        }
        stockSectorDao.batchUpdate(stockSectors);
    }

    public void batchInserts(Map<String,Object> params) {
        List<StockSector> stockSectors = new ArrayList<>();
        String result = JSON.toJSONString(params);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray jsonArray = data.getJSONArray("diff");
        for (int i = 0; i < jsonArray.size(); i++) {
            StockSector stockSector = new StockSector(jsonArray.getJSONObject(i));
            stockSectors.add(stockSector);
        }
        stockSectorDao.batchUpdate(stockSectors);
    }


    /**
     * 获取所有股票代码
     * @return
     */
    public List<StockSector> findAllStockSectorInfo(Map<String,Object> params) {
        return stockSectorDao.findStockSectorInfo(params);
    }
}
